/*
 * DILARANG MENGHAPUS ATAU MENGEDIT COPYRIGHT INI.
 * 
 * Copyright 2008 echo.khannedy@gmail.com. 
 * All rights reserved.
 * 
 * Semua isi dalam file ini adalah hak milik dari echo.khannedy@gmail.com
 * Anda tak diperkenankan untuk menggunakan file atau mengubah file
 * ini kecuali anda tidak menghapus atau merubah lisence ini.
 * 
 * File ini dibuat menggunakan :
 * IDE        : NetBeans
 * NoteBook   : Acer Aspire 5920G
 * OS         : Windows Vista
 * Java       : Java 1.6
 * 
 */
package usu.perpustakaan.buku.widget.operator;

import java.util.Calendar;
import java.util.Date;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import usu.perpustakaan.buku.data.DefaultMember;
import usu.perpustakaan.buku.data.template.Member;
import usu.perpustakaan.buku.util.StringUtil;
import usu.perpustakaan.buku.widget.template.PanelBlackGreen;
import usu.widget.ButtonGlass;
import usu.widget.text.DefaultDocument;

/**
 *
 * @author  usu
 */
public class AddMember extends PanelBlackGreen {

    private static final long serialVersionUID = -1;
    private String errorMessage;
    private Member member;
    private Date date;
    private Calendar cal;
    private Calendar now;

    /** Creates new form BeanForm */
    public AddMember() {
        initComponents();
        initFinal();
    }

    /**
     * 
     * @return
     */
    public String getErrorMessage() {
        return errorMessage;
    }

    /**
     * get member
     * @return member
     */
    public Member getMember() {
        if (textId.getText().trim().equals("")) {
            errorMessage = "Id anggota kosong";
            return null;
        }
        if (textName.getText().trim().equals("")) {
            errorMessage = "Nama anggota kosong";
            return null;
        }
        date = (Date) textBorn.getValue();
        cal = Calendar.getInstance();
        cal.setTime(date);
        now = Calendar.getInstance();
        if (cal.get(Calendar.YEAR) > now.get(Calendar.YEAR)) {
            errorMessage = "Tahun lahir anggota lebih besar dari sekarang";
            return null;
        } else if (cal.get(Calendar.YEAR) == now.get(Calendar.YEAR)) {
            if (cal.get(Calendar.MONTH) > now.get(Calendar.MONTH)) {
                errorMessage = "Bulan lahir anggota lebih besar dari sekarang";
                return null;
            } else if (cal.get(Calendar.MONTH) == now.get(Calendar.MONTH)) {
                if (cal.get(Calendar.DATE) > now.get(Calendar.DATE)) {
                    errorMessage = "Tanggal lahir lebih besar dari sekarang";
                    return null;
                } else if (cal.get(Calendar.DATE) == now.get(Calendar.DATE)) {
                    errorMessage = "Tanggal lahir tak boleh sama dengan sekarang";
                    return null;
                }
            }
        }
        if (textContact.getText().trim().equals("")) {
            errorMessage = "Kontak anggota kosong";
            return null;
        }
        if (textAddress.getText().trim().equals("")) {
            errorMessage = "Alamat anggota kosong";
            return null;
        }
        member = new DefaultMember();
        member.setId(textId.getText());
        member.setAddress(textAddress.getText());
        member.setContact(textContact.getText());
        member.setName(textName.getText());
        member.setBorn(new java.sql.Date(cal.getTimeInMillis()));
        return member;
    }

    /**
     * is contain "
     * @param teks
     * @return
     */
    private boolean containSemiColomn(String teks) {
        return StringUtil.containSemiColomn(teks);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        usu.perpustakaan.buku.widget.template.ViewPortGlass view = new usu.perpustakaan.buku.widget.template.ViewPortGlass();
        textAddress = new javax.swing.JTextArea();
        javax.swing.JLabel jLabel1 = new javax.swing.JLabel();
        javax.swing.JLabel jLabel3 = new javax.swing.JLabel();
        javax.swing.JLabel jLabel4 = new javax.swing.JLabel();
        javax.swing.JLabel jLabel5 = new javax.swing.JLabel();
        javax.swing.JLabel jLabel6 = new javax.swing.JLabel();
        javax.swing.JScrollPane jScrollPane1 = new javax.swing.JScrollPane();
        buttonSubmit = new usu.widget.ButtonGlass();
        usu.widget.ButtonGlass buttonReset = new usu.widget.ButtonGlass();
        buttonCancel = new usu.widget.ButtonGlass();
        textId = new usu.perpustakaan.buku.widget.template.TextBoxGlass();
        textName = new usu.perpustakaan.buku.widget.template.TextBoxGlass();
        textBorn = new usu.perpustakaan.buku.widget.template.FormatedTextBox();
        textContact = new usu.perpustakaan.buku.widget.template.TextBoxGlass();
        javax.swing.JLabel jLabel7 = new javax.swing.JLabel();

        view.setView(textAddress);

        textAddress.setColumns(20);
        textAddress.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        textAddress.setForeground(new java.awt.Color(255, 255, 255));
        textAddress.setLineWrap(true);
        textAddress.setRows(5);
        textAddress.setWrapStyleWord(true);
        textAddress.setBorder(javax.swing.BorderFactory.createEmptyBorder(10, 10, 10, 10));
        textAddress.setFocusAccelerator('A');
        textAddress.setOpaque(false);

        setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0), 3));
        setMinimumSize(new java.awt.Dimension(485, 339));
        setPreferredSize(new java.awt.Dimension(485, 339));

        jLabel1.setDisplayedMnemonic('I');
        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 11));
        jLabel1.setForeground(new java.awt.Color(255, 255, 255));
        jLabel1.setText("Id");

        jLabel3.setDisplayedMnemonic('N');
        jLabel3.setFont(new java.awt.Font("Tahoma", 1, 11));
        jLabel3.setForeground(new java.awt.Color(255, 255, 255));
        jLabel3.setText("Nama");

        jLabel4.setDisplayedMnemonic('L');
        jLabel4.setFont(new java.awt.Font("Tahoma", 1, 11));
        jLabel4.setForeground(new java.awt.Color(255, 255, 255));
        jLabel4.setText("Lahir");

        jLabel5.setDisplayedMnemonic('K');
        jLabel5.setFont(new java.awt.Font("Tahoma", 1, 11));
        jLabel5.setForeground(new java.awt.Color(255, 255, 255));
        jLabel5.setText("Kontak");

        jLabel6.setDisplayedMnemonic('A');
        jLabel6.setFont(new java.awt.Font("Tahoma", 1, 11));
        jLabel6.setForeground(new java.awt.Color(255, 255, 255));
        jLabel6.setText("Alamat");

        jScrollPane1.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
        jScrollPane1.setOpaque(false);
        jScrollPane1.setViewport(view);

        buttonSubmit.setMnemonic('T');
        buttonSubmit.setText("Tambah");
        buttonSubmit.setRoundRect(true);

        buttonReset.setMnemonic('R');
        buttonReset.setText("Reset");
        buttonReset.setRoundRect(true);
        buttonReset.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonResetActionPerformed(evt);
            }
        });

        buttonCancel.setMnemonic('L');
        buttonCancel.setText("Batal");
        buttonCancel.setRoundRect(true);

        textId.setForeground(new java.awt.Color(255, 255, 255));
        textId.setHorizontalAlignment(javax.swing.JTextField.LEFT);
        textId.setFocusAccelerator('I');
        textId.setFont(new java.awt.Font("Tahoma", 1, 11));

        textName.setForeground(new java.awt.Color(255, 255, 255));
        textName.setHorizontalAlignment(javax.swing.JTextField.LEFT);
        textName.setFocusAccelerator('N');
        textName.setFont(new java.awt.Font("Tahoma", 1, 11));

        textBorn.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.DateFormatter(java.text.DateFormat.getDateInstance(java.text.DateFormat.LONG))));
        textBorn.setForeground(new java.awt.Color(255, 255, 255));
        textBorn.setHorizontalAlignment(javax.swing.JTextField.LEFT);
        textBorn.setFocusAccelerator('L');
        textBorn.setFont(new java.awt.Font("Tahoma", 1, 11));
        textBorn.setValue(new java.util.Date());

        textContact.setForeground(new java.awt.Color(255, 255, 255));
        textContact.setHorizontalAlignment(javax.swing.JTextField.LEFT);
        textContact.setFocusAccelerator('K');
        textContact.setFont(new java.awt.Font("Tahoma", 1, 11));

        jLabel7.setFont(new java.awt.Font("Tahoma", 1, 24));
        jLabel7.setForeground(new java.awt.Color(255, 255, 255));
        jLabel7.setText("Tambah Anggota Baru");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(buttonReset, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(buttonCancel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(buttonSubmit, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jLabel7, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 459, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel1)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                .addComponent(jLabel6, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(jLabel5, javax.swing.GroupLayout.DEFAULT_SIZE, 65, Short.MAX_VALUE)
                                .addComponent(jLabel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(jLabel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                        .addGap(4, 4, 4)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(textName, javax.swing.GroupLayout.DEFAULT_SIZE, 390, Short.MAX_VALUE)
                            .addComponent(textBorn, javax.swing.GroupLayout.DEFAULT_SIZE, 390, Short.MAX_VALUE)
                            .addComponent(textContact, javax.swing.GroupLayout.DEFAULT_SIZE, 390, Short.MAX_VALUE)
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 390, Short.MAX_VALUE)
                            .addComponent(textId, javax.swing.GroupLayout.DEFAULT_SIZE, 390, Short.MAX_VALUE))))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel7)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(textId, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(textName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(textBorn, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(textContact, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel6, javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 122, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(buttonSubmit, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(buttonReset, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(buttonCancel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    /**
     * 
     * @return
     */
    public ButtonGlass getButtonCancel() {
        return buttonCancel;
    }

    /**
     * 
     * @return
     */
    public ButtonGlass getButtonSubmit() {
        return buttonSubmit;
    }

    /**
     * reset all text component
     */
    public void reset() {
        textAddress.setText("");
        textBorn.setValue(new Date());
        textContact.setText("");
        textId.setText("");
        textName.setText("");
    }

private void buttonResetActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonResetActionPerformed
// TODO add your handling code here:
    reset();
}//GEN-LAST:event_buttonResetActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    usu.widget.ButtonGlass buttonCancel;
    usu.widget.ButtonGlass buttonSubmit;
    javax.swing.JTextArea textAddress;
    usu.perpustakaan.buku.widget.template.FormatedTextBox textBorn;
    usu.perpustakaan.buku.widget.template.TextBoxGlass textContact;
    usu.perpustakaan.buku.widget.template.TextBoxGlass textId;
    usu.perpustakaan.buku.widget.template.TextBoxGlass textName;
    // End of variables declaration//GEN-END:variables

    /**
     * deklarasi final
     */
    private void initFinal() {
        textName.setDocument(new DefaultDocument(64) {

            private static final long serialVersionUID = -1;

            @Override
            public void insertString(int offs, String str, AttributeSet a) throws BadLocationException {
                if (StringUtil.containDigit(str)) {
                    return;
                }
                if (containSemiColomn(str)) {
                    return;
                }
                super.insertString(offs, str, a);
            }
        });
        textAddress.setDocument(new DefaultDocument() {

            private static final long serialVersionUID = -1;

            @Override
            public void insertString(int offs, String str, AttributeSet a) throws BadLocationException {
                if (containSemiColomn(str)) {
                    return;
                }
                super.insertString(offs, str, a);
            }
        });
        textContact.setDocument(new DefaultDocument(64) {

            private static final long serialVersionUID = -1;

            @Override
            public void insertString(int offs, String str, AttributeSet a) throws BadLocationException {
                if (containSemiColomn(str)) {
                    return;
                }
                super.insertString(offs, str, a);
            }
        });
        textId.setDocument(new DefaultDocument(10) {

            private static final long serialVersionUID = -1;

            @Override
            public void insertString(int offs, String str, AttributeSet a) throws BadLocationException {
                if (containSemiColomn(str)) {
                    return;
                }
                super.insertString(offs, str, a);
            }
        });
    }
}
